/*
 * =====================================================================================
 *
 *       Filename:  cl_thread.hxx
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  12/22/2010 09:45:32 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Ken Chen (Ken), chen_kenz@emc.com
 *        Company:  EMC
 *
 * =====================================================================================
 */

#ifndef _CL_THREAD_HXX_
#define _CL_THREAD_HXX_

#include <pthread.h>
#include "cl_callback.hxx"

namespace neo
{
class CLThread
{
public:
  enum ThreadState
  {
    NOT_RUNNING,
    RUNNING
  };

  CLThread(const CLCallbackFunctor& cb, bool isJoinable = true, bool isSysScope = true, size_t stackSize = 0);
  ~CLThread();
  bool start(void);
  bool join(void);
  bool isCurThread(void) const;
  
private:
  static void* doStart(void* arg);
  void run(void);
  void setScope(void); 
  void setJoinable(void);
  void setStackSize(void);

private:
	CLThread(const CLThread&);
	CLThread& operator=(const CLThread);

private:
	CLCallbackFunctor m_callback;
  pthread_t m_thrId;
  pthread_attr_t m_thrAttr;
  ThreadState m_thrState;
	bool m_isJoinable;
	bool m_isSysScope;
	size_t m_stackSize;
};

namespace CurrentThread
{
  pid_t clThreadId(void);
  bool clIsMainThread(void);
}

} // neo

#endif //_CL_THREAD_HXX_

